package xyz.bali16.module.article.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.module.article.entity.Tag;
import xyz.bali16.module.article.model.tag.TagAddParam;
import xyz.bali16.module.article.model.tag.TagPageParam;
import xyz.bali16.module.article.model.tag.TagUpdateParam;
import xyz.bali16.module.article.service.TagService;
import javax.validation.Valid;
import static xyz.bali16.application.core.model.Result.*;
import static xyz.bali16.module.article.utils.ResultUtils.errorLog;


/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@RestController
@RequestMapping(BaseController.API+"tag")
@Api(tags = "标签接口")
@AllArgsConstructor
public class TagController extends BaseController{
    public static final Logger logger = LoggerFactory.getLogger(TagController.class);
    @Autowired
    TagService tagService;
    @GetMapping
    @ApiOperation("分页条件查询标签")
    //@PreAuthorize("@auth.permission('plugin:article:tag:query')")
    public Result<IPage<Tag>> pageQuery(@Valid TagPageParam param) {
        IPage<Tag> tagIPage = tagService.getPage(param);
        return success(ApiEnum.GET_SUCCESS, tagIPage);
    }

    @PostMapping
    //@PreAuthorize("@auth.permission('plugin:article:tag:query')")
    @ApiOperation("添加标签")
    public Result<String> add(@RequestBody @Valid TagAddParam param) {
        try {
            tagService.addTag(param);
            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "新增标签 '{}' 信息失败 {}", param.getName(), e.getMessage());
            return failure(ApiEnum.ADD_ERROR, "新增标签失败", e);
        }
    }

    @PutMapping
    //@PreAuthorize("@auth.permission('plugin:article:tag:update')")
    @ApiOperation("修改标签")
    public Result<String> update(@RequestBody @Valid TagUpdateParam param) {
        try {
            tagService.updateTag(param);
            return response(ApiEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "更新标签 '{}' 信息失败 {}", param.getId(), e.getMessage());
            return failure(ApiEnum.UPDATE_ERROR, "更新标签失败", e);
        }
    }

    @DeleteMapping("/{tagId}")
    //@PreAuthorize("@auth.permission('plugin:article:tag:delete')")
    @ApiOperation("删除标签")
    @ApiImplicitParam(name = "tagId", value = "标签id", paramType = "path", required = true)
    public Result<String> deleteTag(@PathVariable("tagId") String tagId) {
        try {
            tagService.delete(tagId);
            return response(ApiEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "删除标签 '{}' 失败. {}", tagId, e.getMessage());
            return failure(ApiEnum.DELETE_ERROR, e);
        }
    }

}
